package com.fruitshop.manager;

import com.fruitshop.manager.base.BaseManager;
import com.fruitshop.mapper.AdminMapper;
import com.fruitshop.model.Admin;
import com.fruitshop.model.Command;
import com.fruitshop.util.ConsoleUtils;
import com.fruitshop.util.DBUtil;
import com.fruitshop.util.PrintUtils;

import java.util.List;
import java.util.Scanner;

public class AdminRegisterManager extends BaseManager {
    private static final String ADMIN_INPUT="1";

    public AdminRegisterManager(String title, List<Command> commandList) {
        super(title, commandList);
    }

    @Override
    protected String processCommand(String command) {
        switch (command) {
            case ADMIN_INPUT:
                return showAdminInput();
        }
        return command;
    }

    private String showAdminInput() {
        Scanner scanner = new Scanner(System.in);
        String name= ConsoleUtils.readString(scanner, "请输入管理员名");
        String account=ConsoleUtils.readString(scanner, "请输入管理员账号");
        String password="";
        while (true){
            //输入密码
            password=ConsoleUtils.readString(scanner, "请输入密码");
            //输入确认密码
            String rePassword = ConsoleUtils.readString(scanner, "请输入确认密码");
            if(password.equals(rePassword)){
                break;
            }
            PrintUtils.printError("两次输入的密码不一致,请重新输入");
        }

        Admin admin = new Admin();
        admin.setName(name);
        admin.setAccount(account);
        admin.setPassword(password);

        AdminMapper adminMapper = DBUtil.getMapper(AdminMapper.class);

        Admin byAccount = adminMapper.selectByAccount(account);

        if(byAccount!=null){
            PrintUtils.printError("该账号已存在");
            return NORMAL_EXIT;
        }
        adminMapper.insert(admin);

        PrintUtils.printHighLight("注册成功");

        return NORMAL_EXIT;
    }
}
